package com.xms.schema.service.optionset;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.service.IDependencyChecker;
import com.xms.dependency.service.IDependencyDeleterService;
import com.xms.event.core.IEventPublisher;
import com.xms.schema.core.OptionSetDefaults;
import com.xms.schema.entity.OptionSet;
import com.xms.schema.event.OptionSetDeletedEvent;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * OptionSetDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@CacheConfig(cacheNames = "schema.optionset", keyGenerator = "CacheKeyGenerator")
@Service
public class OptionSetDeleterService extends DeleterService<OptionSet> implements IOptionSetDeleterService {
    private IDependencyDeleterService _dependencyDeleterService;
    private IDependencyChecker _dependencyChecker;
    private IEventPublisher _eventPublisher;

    @Autowired
    public OptionSetDeleterService(IDependencyDeleterService dependencyDeleterService,
                                   IDependencyChecker dependencyChecker,
                                   IEventPublisher eventPublisher) {
        _dependencyDeleterService = dependencyDeleterService;
        _dependencyChecker = dependencyChecker;
        _eventPublisher = eventPublisher;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean delete(OptionSet entity) throws SQLException, XmsDependencyException {
        Assert.notNull(entity);
        //检查依赖项
        _dependencyChecker.checkAndThrow(OptionSetDefaults.MODULE_NAME, entity.getOptionsetid());
        boolean success = super.deleteById(entity.getOptionsetid());
        if (success) {
            //删除依赖项
            _dependencyDeleterService.deleteByDependentId(OptionSetDefaults.MODULE_NAME, entity.getOptionsetid());
            //删除事件
            _eventPublisher.publish(new OptionSetDeletedEvent(OptionSetDefaults.MODULE_NAME, entity));
            return true;
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        OptionSet entity = super.getById(id);
        if (entity == null)
            return false;
        return this.delete(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<OptionSet> queryWrapper) throws SQLException, XmsDependencyException {
        List<OptionSet> deleteds = super.getService().list(queryWrapper);
        if (CollectionUtil.isEmpty(deleteds))
            return false;
        for (OptionSet deleted : deleteds) {
            this.delete(deleted);
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws SQLException, XmsDependencyException {
        List<OptionSet> deleteds = super.getService().listByIds(idList);
        if (CollectionUtil.isEmpty(deleteds))
            return false;
        for (OptionSet deleted : deleteds) {
            this.delete(deleted);
        }
        return true;
    }
}
